﻿using System;
using Gtk;


public partial class MainWindow: Gtk.Window
{
	String contentsTA;
	Boolean ready=false;

	public MainWindow () : base (Gtk.WindowType.Toplevel)
	{
		Build ();
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
	/*
		NOTES:
		contentsTA = textview1.Buffer.Text;
		textview1.Buffer.Text = contentsTA;
		//System.IO.File.WriteAllLines(@"/home/myuser/someText.txt", contentsTA);
	*/
	//__________________________________________________________________________________________________________
	//save as
	//__________________________________________________________________________________________________________
	protected void saveAs (object sender, EventArgs e)
	{
		FileChooserDialog chooser = new FileChooserDialog (
			"Please choose a name and path for file ...",
			this,
			FileChooserAction.Save,
			"cancel", ResponseType.Cancel,
			"save", ResponseType.Accept);	
		if (chooser.Run () == (int)ResponseType.Accept) {
			contentsTA = textview1.Buffer.Text;
			String[] val=new []{ contentsTA };
			System.IO.File.WriteAllLines (chooser.Filename, val);
			string[] forsaveas=new[]{chooser.Filename};
			System.IO.File.WriteAllLines (@".ssourcefile.txt",forsaveas);//write name of file so that it can be saved also after
			ready = true;
		
		}
		chooser.Destroy ();
	
	}
	//__________________________________________________________________________________________________________
	//Open
	//__________________________________________________________________________________________________________
	protected void open (object sender, EventArgs e)
	{
		int width, height;
		this.GetDefaultSize (out width, out height);
		this.Resize (width, height);
		// Create and display a fileChooserDialog
		FileChooserDialog chooser = new FileChooserDialog (
			                            "Please select a file to open ...",
			                            this,
			                            FileChooserAction.Open,
			                            "cancel", ResponseType.Cancel,
			                            "view", ResponseType.Accept);

		if (chooser.Run () == (int)ResponseType.Accept) {
			System.IO.StreamReader file =
				System.IO.File.OpenText (chooser.Filename);
			textview1.Buffer.Text = file.ReadToEnd ();
			this.Title = " ssource editor -- " + chooser.Filename.ToString ();
			this.Resize (640, 480);
			file.Close ();
		} 
		chooser.Destroy ();	
	}
	//__________________________________________________________________________________________________________
	//About dialog
	//__________________________________________________________________________________________________________
	protected void about (object sender, EventArgs e)
	{
		AboutDialog about = new AboutDialog ();
		about.ProgramName = "Mola-Mola Editor";
		about.Version = "1.0.0";
		about.Run ();
		about.Destroy ();
	}
	//__________________________________________________________________________________________________________
	//save function
	//__________________________________________________________________________________________________________
	protected void quickSave (object sender, EventArgs e)
	{
		if (ready == true) {
			contentsTA = textview1.Buffer.Text;//read value from textView
			String[] val = new []{ contentsTA };//now convert it from string to string[]
			System.IO.StreamReader file = System.IO.File.OpenText (@".ssourcefile.txt");//openfile containing path
			String filePath = file.ReadToEnd ();//red file and save path
			System.IO.File.WriteAllLines (filePath, val);//now write it to a file
		} else {
			AboutDialog about = new AboutDialog ();
			about.ProgramName = "first save as!";
			about.Run ();
			about.Destroy ();
		
		}


	}
}
